package com.xinwei.linearExercise;

public abstract class AbstractList<E> implements List<E> {
    //元素的数量
    protected int size = 0;

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;//这里相当于判断size是否为0，并返回true或false
    }

    public boolean contains(E element) {
        return indexof(element) != ELEMENT_NOT_FOUND;
    }

    public void add(E element) {
        add(size, element);
    }

    // protected 代表子类中能用
    protected void outOfBounds() {
        throw new IndexOutOfBoundsException("索引越界");
    }

    protected void rangeCheck(int index) {
        if (index < 0 && index >= size) outOfBounds();
    }

    //添加和其他的不一样，允许在size处添加
    protected void rangeCheckForAdd(int index) {
        if (index < 0 && index > size) outOfBounds();
    }
}
